package com.hsmw.api.controller.biz;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwForcemanItemMapper;
import com.hsmw.api.mapper.HsmwOrderMapper;
import com.hsmw.api.service.*;
import com.hsmw.api.vo.RetailerOrForemanInfoVO;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.AuditStatusEnum;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.constant.UserTypeEnum;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @author WD
 */
@Api(tags = "BIZ-散户审核管理")
@RestController
@RequestMapping("/api/foreman")
public class BizForemanController extends BaseController<HsmwAuditService, HsmwAudit> {




	@Autowired
	private HsmwVehicleService hsmwVehicleService;

	@Autowired
	private HsmwItemService hsmwItemService;


	@Autowired
	private HsmwLeasingCompanyService hsmwLeasingCompanyService;


	@Autowired
	private CurrentHsmwUserUtils currentHsmwUserUtils;




	@Autowired
	private HsmwOrderMapper hsmwOrderMapper;



	@Autowired
	private HsmwForcemanItemMapper hsmwForcemanItemMapper;




	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;

	/**
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取工长的列表", notes = "获取工长的列表", produces = "application/json")
	@ApiImplicitParam(value = "queryParam" ,name = "查询关键字")
	@PostMapping("/getForemanList")
	public DataResponse getForemanList(@RequestBody @Valid BasePage basePage) {
		HashMap param = new HashMap();
		IPage<RetailerOrForemanInfoVO> retailerUserInfoVOS = hsmwHtnUserService.getForemanList(basePage);
		return new DataResponse(retailerUserInfoVOS);
	}


	@ApiOperation(value = "新增工长 ", notes = "新增工长", produces = "application/json")
	@PostMapping("/addForemanUser")
	public DataResponse addForemanUser(@RequestBody HsmwHtnUser hsmwUser) {
		LoginUser currentUser = currentHsmwUserUtils.getCurrentUserInfo();
		List<HsmwHtnUser> users = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
				.eq(HsmwHtnUser::getUsername,hsmwUser.getUsername()));
		if(CollectionUtil.isNotEmpty(users)){
			return new DataResponse(CommonConstants.ResponseStatus.FAIL, "工长电话在已经存在，请重新输入！");
		}
		String itemIds = hsmwUser.getItemId();
		hsmwUser.setType(UserTypeEnum.FOREMAN.getValue());
		hsmwUser.setEnabled(true);
		hsmwUser.setForemanType(CommonConstants.FORE_TYPE_1);
		hsmwUser.setCreateUser(currentUser.getId());
		hsmwUser.setItemId(null);
		String userId = CommonUtils.generateId(hsmwUser);
		hsmwUser.setId(userId);
		hsmwHtnUserService.save(hsmwUser);
		//保存信息到 项目和工长的关联表中
		if(StringUtils.isNotBlank(itemIds)){
			String[] itemIdss = itemIds.split(",");
			for(String itemId :itemIdss){
				HsmwForcemanItem forcemanItem = new HsmwForcemanItem();
				forcemanItem.setItemId(itemId);
				forcemanItem.setForcemanId(userId);
				hsmwForcemanItemMapper.insert(forcemanItem);
			}
		}


		return new DataResponse();
	}

	/**
	 * 修改工长信息 获取项目下拉列表
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "修改工长信息 获取项目下拉列表", notes = "获取项目下拉列表  ", produces = "application/json")
	@PostMapping("/getAllHsmwItemSelect")
	public DataResponse getHsmwItemSelect() {
		List<HsmwItem> data = hsmwItemService.list(Wrappers.lambdaQuery(HsmwItem.class)
				.eq(HsmwItem::getAuditStatus, AuditStatusEnum.RECEIVED.getValue())
				.eq(HsmwItem::getItemStatus,CommonConstants.NO));
		return new DataResponse(data);
	}


	/**
	 * 分页
	 *
	 * @param
	 * @return
	 */
		@ApiOperation(value = "获取项目列表，", notes = "获取司机分页列表", produces = "application/json")
	@PostMapping("/getHsmwItemList")
	public DataResponse getHsmwItemList() {
		List<HsmwItem> data = hsmwItemService.list(new LambdaQueryWrapper<HsmwItem>()
				.eq(HsmwItem::getAuditStatus, AuditStatusEnum.RECEIVED.getValue()));
		return DataResponse.success(data);
	}

/*

	*/
/**
	 * 分页
	 *
	 * @param
	 * @return
	 *//*

	@ApiOperation(value = "处理项目数据，", notes = "获取司机分页列表", produces = "application/json")
	@PostMapping("/processItemData")
	public DataResponse processItemData() {
		List<HsmwUser> orderList =hsmwUserService.itme_user_list(new LambdaQueryWrapper<HsmwUser>()
				.eq(HsmwUser::getType,UserTypeEnum.FOREMAN.getValue())
				.eq(HsmwUser::getForemanType,CommonConstants.YES));
		for(HsmwUser order:orderList){
			HsmwForcemanItem item = new HsmwForcemanItem();
			item.setItemId(order.getItemId());
			item.setForcemanId(order.getId());
			item.setCreateTime(LocalDateTime.now());
			hsmwForcemanItemMapper.insert(item);
		}
		return DataResponse.success(1);
	}
*/




}
